package cn.zld.sh.sh.ui.userfragment;

import android.arch.lifecycle.ViewModelProviders;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.zld.sh.sh.LoginActivity;
import cn.zld.sh.sh.MainActivity;
import cn.zld.sh.sh.R;
import cn.zld.sh.sh.url.HttpUrls;
import cn.zld.sh.sh.util.OkHttpClientManager;
import cn.zld.sh.sh.util.SharedUtils;
import cn.zld.sh.sh.util.ToastUtils;

public class UserFragment extends Fragment {

    private UserFragmentViewModel mViewModel;

    private TextView tvMobile;
    private TextView tvYqm;
    private TextView tvVip;
    private Button btnRerushVip;
    private Context myContext;
    private JSONObject uInfo;
    private Handler handler = new Handler();

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.user_fragment_fragment, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(UserFragmentViewModel.class);
        myContext = getContext();
        // TODO: Use the ViewModel
        init();
        getUinfo(true);
    }

    private void init() {
        tvMobile = getActivity().findViewById(R.id.uinfoMobile);
        tvVip = getActivity().findViewById(R.id.uinfoVip);
        tvYqm = getActivity().findViewById(R.id.uinfoYqm);
//        tvYqm.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
//        tvYqm.getPaint().setAntiAlias(true);//抗锯齿
        tvYqm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(myContext.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", tvYqm.getText());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtils.showLongToast(getActivity(), "验证码" + tvYqm.getText() + "已复制！");
            }
        });

        btnRerushVip = getActivity().findViewById(R.id.reRushvip);
        btnRerushVip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(myContext, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                myContext.startActivity(intent);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        });
    }

    private Runnable uInfoTask = new Runnable() {
        @Override
        public void run() {
            if (uInfo != null) {
                try {
                    tvMobile.setText(uInfo.getString("mobile"));
                    tvYqm.setText(uInfo.getString("id"));

                    JSONArray ja = uInfo.getJSONArray("vip");

                    if(ja.length() <= 0){
                        SharedUtils.putInt("vipStatus", 0);
                        return;
                    }
                    JSONObject jo2 = ja.getJSONObject(0);
                    tvVip.setText(jo2.getString("endTime"));
                    SharedUtils.putString("vipEndTime", jo2.getString("endTime"));
                    if (jo2.getInt("status") == 1) {
                        tvVip.setTextColor(Color.parseColor("#2EFE2E"));
                    } else {
                        tvVip.setTextColor(Color.parseColor("#FF0000"));
                    }

                } catch (Exception e) {
                    return;
                }
            }
        }
    };

    public void getUinfo(final boolean setText) {
        new Thread() {

            public void run() {
                try {
                    OkHttpClientManager.Param[] params = new OkHttpClientManager.Param[]{
                            new OkHttpClientManager.Param("token", SharedUtils.getString("token"))
                    };
                    Response response = OkHttpClientManager.post(HttpUrls.USERINFO, params);

                    String html = response.body().string();

                    JSONObject jo = new JSONObject(html);

                    if (jo.getInt("code") == 0) {
                        uInfo = jo.getJSONObject("data");
                        JSONArray ja = uInfo.getJSONArray("vip");

                        if(setText){
                            handler.post(uInfoTask);
                        }

                        if(ja.length() <= 0){
                            SharedUtils.putInt("vipStatus", 0);
                            return;
                        }
                        JSONObject jo2 = ja.getJSONObject(0);

                        SharedUtils.putInt("vipStatus", jo2.getInt("status"));
                        SharedUtils.putString("vipEndTime", jo2.getString("endTime"));

                    }else{
                        SharedUtils.putInt("vipStatus", 0);
                        SharedUtils.putString("token", "");
                        Intent intent = new Intent();
                        intent.setClass(myContext, LoginActivity.class);
                        startActivity(intent);
                    }

                } catch (Exception e) {
                    return;
                }
            }

        }.start();
    }

}
